package cn.edu.pku.ss.situationawareperceiver;

import cn.edu.pku.ss.situationawareperceiver.dbrecords.ContextRequestRecord;
import cn.edu.pku.ss.situationawareperceiver.dbrecords.ContextShareRecord;
import cn.edu.pku.ss.situationawareperceiver.dbrecords.SRARecord;
import cn.edu.pku.ss.situationawareperceiver.dbrecords.ContextRequestRecord;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

public class DBAdapter {
	private static final String DB_NAME = "sra.db";
	private static final String DB_TABLE_SRARECORD = "SRARecord";
	private static final String DB_TABLE_CONTEXTSHARERECORD = "ContextShareRecord";
	private static final String DB_TABLE_CONTEXTREQUESTRECORD = "ContextRequestRecord";
	private static final int DB_VERSION = 1;
	
	private SQLiteDatabase db;
	private final Context context;
	private DBOpenHelper dbOpenHelper;
	
	private static class DBOpenHelper extends SQLiteOpenHelper{

		private static final String DB_CREATE_SRARECORD = "create table " + DB_TABLE_SRARECORD
				+ " (id integer primary key autoincrement, triggerSituationId integer,"
				+ " resultSituationId text not null, intermediateNodes text not null,"
				+ " abductionCount integer, startTime text not null, endTime text not null,"
				+ " timeCost integer, validateCountClipped integer, validateCountNoClip integer);";
		
		private static final String DB_CREATE_CONTEXTSHARERECORD = "create table " + DB_TABLE_CONTEXTSHARERECORD
				+ " (id integer primary key autoincrement, requesterId text not null,"
				+ " providerId text not null, receiveTime text not null,"
				+ " contextTypeInt integer, truthValue text not null);";
		
		private static final String DB_CREATE_CONTEXTREQUESTRECORD = "create table " + DB_TABLE_CONTEXTREQUESTRECORD
				+ " (id integer primary key autoincrement, requesterId integer,"
				+ " queringContextTypeId integer, retriveTypeInt integer,"
				+ " requestTime text not null);";
		
		public DBOpenHelper(Context context, String name, CursorFactory factory, int version) {
			super(context, name, factory, version);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(DB_CREATE_SRARECORD);
			db.execSQL(DB_CREATE_CONTEXTSHARERECORD);
			db.execSQL(DB_CREATE_CONTEXTREQUESTRECORD);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL("DROP TABLE IF EXISTS " + DB_CREATE_SRARECORD);
			db.execSQL("DROP TABLE IF EXISTS " + DB_CREATE_CONTEXTSHARERECORD);
			db.execSQL("DROP TABLE IF EXISTS " + DB_CREATE_CONTEXTREQUESTRECORD);
			onCreate(db);
		}
		
	}
	
	public DBAdapter(Context _context){
		context = _context;
	}
	
	public void open() throws SQLiteException{
		dbOpenHelper = new DBOpenHelper(context, DB_NAME, null, DB_VERSION);
		try{
			db = dbOpenHelper.getWritableDatabase();
		}catch (SQLiteException ex) {
			db = dbOpenHelper.getReadableDatabase();
		}
	}
	
	public void close(){
		if(db != null)
		{
			db.close();
			db = null;
		}
	}
	
//	public long insert(ReasoningRecord rr){
//		ContentValues newValues = new ContentValues();
//		newValues.put(TRIGGER, rr.tiggerID);
//		newValues.put(RESULT, rr.resultID);
//		newValues.put(INTER, rr.intermediateID);
//		return db.insert(DB_TABLE, null, newValues);
//	}
	
	
	/**
	 * Insert records into tables 
	 */
	public void insertContextShareRecord(ContextShareRecord csRec){
		ContentValues newValues = new ContentValues();
		newValues.put("requesterId", csRec.getRequesterId());
		newValues.put("providerId", csRec.getProviderId());
		newValues.put("receiveTime", csRec.getReceiveTime());
		newValues.put("contextTypeInt", csRec.getContextTypeInt());
		newValues.put("truthValue", csRec.getTruthValue());
		open();
		db.insert(DB_TABLE_CONTEXTSHARERECORD, null, newValues);
		close();
	}
	public void insertContextRequestRecord(ContextRequestRecord srRec){
		ContentValues newValues = new ContentValues();
		newValues.put("requesterId", srRec.getRequesterId());
		newValues.put("queringContextTypeId", srRec.getQueringContextTypeId());
		newValues.put("retriveTypeInt", srRec.getRetriveTypeInt());
		newValues.put("requestTime", srRec.getRequestTime());
		open();
		db.insert(DB_TABLE_CONTEXTREQUESTRECORD, null, newValues);
		close();
	

	}
	
	public void insertSRARecord(SRARecord sraRec){
		ContentValues newValues = new ContentValues();
		newValues.put("triggerSituationId", sraRec.getTriggerSituationId());
		newValues.put("resultSituationId", sraRec.getResultSituationId());
		newValues.put("intermediateNodes", sraRec.getIntermediateNodes());
		newValues.put("abductionCount", sraRec.getAbductionCount());
		newValues.put("startTime", sraRec.getStartTime());
		newValues.put("endTime", sraRec.getEndTime());
		newValues.put("timeCost", sraRec.getTimeCost());
		newValues.put("validateCountClipped", sraRec.getValidateCountClipped());
		newValues.put("validateCountNoClip", sraRec.getValidateCountNoClip());
		open();
		db.insert(DB_TABLE_SRARECORD, null, newValues);
		close();
	}
}
